﻿using System;
using System.Collections.Generic;
using FinPlusComponents;
using QLNet;
using p = FinPlusAnalytics.QLConvParser;
using u = FinPlusUtility.Utility;

namespace FinPlusAnalytics
{
    public class YieldCurve : FinPlusComponent
    {
        public string Name { get; private set; }
        public PiecewiseYieldCurve PiecewiseYieldCurve { get; private set; }
        public SwapEngine SwapEngine { get; private set; }

        //construct
        public YieldCurve(string marketName, string name, string discountCurveName, DateTime settlementDate, string rates, string dayCount, string holidays, double tolerance = 1.0e-15)
        {
            Name = name;
            var market = Markets.Instance.GetMarket(marketName);
            var calendar = p.Calendar(holidays);
            var instruments = new List<RateHelper>();
            
            var res = u.StringToArray1d(rates, ',');
            foreach (string r in res)
            {
                var rateHelper = market.GetRateHelper(r);
                if(rateHelper!= null)
                    instruments.Add(rateHelper);
            }

            PiecewiseYieldCurve = new PiecewiseYieldCurve<Discount, LogLinear>(settlementDate, instruments, p.DayCount(dayCount), new List<Handle<Quote>>(), new List<Date>(), tolerance);
            market.SetCurve(name, PiecewiseYieldCurve);
            SwapEngine = new SwapEngine(market, name, discountCurveName == "" ? name : discountCurveName);
        }
    }
}
